package cn.ac.siom.query.action;

import cn.ac.siom.query.service.IPatents_infoService;
import cn.ac.siom.query.service.IaccessService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.Accessinfo;
import cn.ac.siom.query.vo.Patents_info;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/home/*")
public class Patents_infoAction extends DefaultAction {

    @Resource
    private IPatents_infoService patents_infoService;
    @Resource
    private IaccessService iaccessService;

    @RequestMapping(value = "patents_info_list")
    public void list(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            insertAccessinfo(request,"专利文献");
            super.printObjectToList(response, "allPatents_info", this.patents_infoService.list());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @RequestMapping(value="findNO_Patents_info", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Patents_info> findNO(Integer i_id, HttpServletRequest request, Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/");
        }else{
            accessinfo.setUrl(url);
            accessinfo.setName("专利详情页");
        }
        accessinfo.setIp(ip);
        this.iaccessService.insertOne(accessinfo);
        List<Patents_info>  patents_infos = this.patents_infoService.list_NO(i_id);
        return patents_infos;
    }

    @ResponseBody
    @RequestMapping(value="findPage_Patents_info", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(HttpServletRequest request, HttpServletResponse response, Integer rownum, Integer page_id,Accessinfo accessinfo) throws SQLException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/");
        }else{
            accessinfo.setUrl(url);
            accessinfo.setName("专利分页");
        }
        accessinfo.setIp(ip);
        this.iaccessService.insertOne(accessinfo);
        List<Patents_info>  patents_infos = (List<Patents_info>) this.patents_infoService.list_page(rownum,page_id);
        Integer cont=this.patents_infoService.list_count();
        Map<String,Object> map = new HashMap<>();
        map.put("patents_infos",patents_infos);
        map.put("cont",cont);
        map.put("page_id",page_id);
        return map;
    }

    @Override
    public String getText() {
        return null;
    }
}
